import simg from './images/start.png'
import eimg from './images/end.png'
import './styles/Mappathset.less'

class Mappathset {


	_path = {}
	_delbtn = {}
	_num = 0
	_line = null
	_isOpen = false

	constructor({map,data}){
		this.map = map;
		this.data = data;
		this._init();
	}

	_init(){
		this._line = new PathSimplifier({
        zIndex: 100,
        map: this.map, //所属的地图实例
        getPath:(pathItem)=>{
        	return pathItem.path;
        },
        autoSetFitView:false,
        getHoverTitle:()=>null,
        renderOptions: {
        		// eventSupport:false,
        		eventSupportInvisible:false,
            getPathStyle:(pathItem, zoom)=>{
            	return  {
	            		pathLineStyle:{
		                strokeStyle: '#909e9e',
		                lineWidth: 6,
		                dirArrowStyle: true
		            }
	          	}
            }
        }
    });
		this.map.on('click',e=>{
			if(!this._isOpen)return;
			this._createPoint(e.lnglat);
		});
		if(this.data&&this.data.length){
			this._initData();
		}
	}

	_createPoint(lnglat,noEdit){
		let path = this.getPath();
		let m;
		if(path.length===0){
			m = this._addStart(lnglat);
		}else{
			if(path.length>1){
				let _p = path[path.length-1];
				_p.type = 0;
				_p.m.setIcon(new AMap.Icon({
            size: new AMap.Size(12, 12),//图标大小
            image: "https://webapi.amap.com/images/pldot.png",
            imageOffset:new AMap.Pixel(-24, 0)
        }));
				_p.m.setOffset(new AMap.Pixel(-6, -6));
			}
			m = this._addEnd(lnglat);
		}
		let type = path.length===0?1:2;
		this._path[`m${this._num}`] = {k:`m${this._num}`,type,m};
		this._delinit(this._num,lnglat);
		this._num++;
		if(path.length>0){
			this._setPathEdit();
		}
	}

	_initData(){
		this.data.forEach((v,i)=>{
			let lnglat = new AMap.LngLat(v.lng,v.lat);
			let m,type;
			if(i === 0){
				m = this._addStart(lnglat);
				type = 1;
			}else if(i === this.data.length-1){
				m = this._addEnd(lnglat);
				type = 2;
			}else{
				type = 0;
				m = lnglat;
			}
			this._path[`m${this._num}`] = {k:`m${this._num}`,type,m};
			this._num++;
		});
	
		this._setPath();
	}


	_addStart(lnglat){
		return new AMap.Marker({
			map:this.map,
			icon:new AMap.Icon({
            imageSize:new AMap.Size(32/1.5,53/1.5),
            size: new AMap.Size(32, 53),
            image: simg
        }),
			position:lnglat
		})
	}

	_addEnd(lnglat){
		return new AMap.Marker({
			map:this.map,
			icon:new AMap.Icon({
            imageSize:new AMap.Size(32/1.5,53/1.5),
            size: new AMap.Size(32, 53),
            image: eimg
        }),
			position:lnglat
		});
	}

	_setPath(){
		let path = this.getPath();
		this._line.setData([{path:path.map(v=>v.type===0?v.m:v.m.getPosition())}])
		this._line.setFitView()
	}

	_setPathEdit(){
		let path = this.getPath();
		this._line.setData([{path:path.map(v=>v.m.getPosition())}])

	}

	_delinit(i,lnglat){
		let div = document.createElement('div');
		div.innerHTML = `<div class="pathset-btn">删除</div>`
		this._delbtn[`d${i}`] = new AMap.Marker({
			map:this.map,
			content:div,
			position:lnglat,
			offset:new AMap.Pixel(-20, 0)
		});

		div.addEventListener('click',()=>{
			this._delbtn[`d${i}`].setMap(null);
			delete this._delbtn[`d${i}`];
			let p = this._path[`m${i}`]
			p.m.setMap(null);
			let list = this.getPath();
			if(p.type===2){
				let p2 = list[list.length-2];
				if(p2&&p2.type===0){
					this._path[p2.k] = {k:p2.k,type:2,m:this._addEnd(p2.m.getPosition())};
					p2.m.setMap(null);
				}	
			}else if(p.type === 1){
				let p2 = list[1];
				if(p2){
					this._path[p2.k] = {k:p2.k,type:1,m:this._addStart(p2.m.getPosition())};
					p2.m.setMap(null);
				}
				
			}
			delete this._path[`m${i}`];
			
			this._setPathEdit();
		});

	}

	getPath(){
		let s = [];
		for(let k in this._path){
			s.push(this._path[k]);
		}
		 ;
		return s;
	}

	_remove(){
		for(let k in this._path){
			this._path[k].m.setMap&&this._path[k].m.setMap(null);
		}
		for(let k in this._delbtn){
			this._delbtn[k].setMap&&this._delbtn[k].setMap(null);
		}
		if(this._line){
			this._line.setData([]);
		}
		this._path = {};
		this._delbtn = {};
		this._num = 0;
		this._isOpen = false;
	}

	create(){
		this._isOpen = true;
	}

	reset(){
		this._remove();
	}

	getFormData(){
		let s = [];
		for(let k in this._path){
			let lnglat = this._path[k].type===0?this._path[k].m.getPosition?this._path[k].m.getPosition():this._path[k].m:this._path[k].m.getPosition();
			s.push({lng:lnglat.lng,lat:lnglat.lat});
		}
		return s.length?s:null;
	}
}
export default Mappathset;